import User from '@/select/user/User.vue';
/**
* @Author: 李晨光
* @Date: 2019/12/18
* @Version: 1.0
* @Last Modified by: 李晨光
* @Last Modified time: 2019/12/18
**/
<comment>
  # 积分明细
</comment>
<template>
  <div id="IntegrationCode" class="p-box">
    <div class="p-return">
      <mu-appbar color="primary" class="lan-header" style="box-shadow:none">
        <mu-button icon slot="left" v-close>
          <i class="iconfont angle-left iconangle-left"></i>
          <span class="lan-header-back">返回</span>
        </mu-button>兑换码
      </mu-appbar>
    </div>
    <div class="p-content">
      <dl>
        <dt>
          <QRcode width="200" height="200" :content="content" :load="false"></QRcode>
        </dt>
        <dd>
          <p class="des">兑换积分二维码</p>
          <p class="fen">当前可兑换积分{{leftScore}}</p>
        </dd>
      </dl>
    </div>
  </div>
</template>

<script>
export default {
  name: "IntegrationCode",
  components: {},
  props: {},
  data() {
    return {
      leftScore: 0,
      content: null
    };
  },
  computed: {},
  created() {
    this.initPage();
  },
  mounted() {
    let timer = setInterval(() => {
      this.getLeftSoreLun();
    }, 2000);
    // 通过$once来监听定时器，在beforeDestroy钩子可以被清除。
    this.$once("hook:beforeDestroy", () => {
      clearInterval(timer);
      timer = null;
    });
  },
  watch: {},
  methods: {
    initPage() {
      this.getLeftSore();
    },
    getLeftSore() {
      this.$openLoading();
      this.$A
        .Go("get", "/safety/userScore/myScore", {
          userId: this.$A.GS("userInfo")["userId"]
        })
        .then(res => {
          this.$closeLoading();
          if (res.code == 200) {
            if (res.body) {
              this.leftScore = res.body.leftScore;
              this.createCode(this.leftScore);
            } else {
              this.createCode(0);
            }
          } else {
            this.createCode(0);
          }
        });
    },
    getLeftSoreLun() {
      this.$A
        .Go("get", "/safety/userScore/myScore", {
          userId: this.$A.GS("userInfo")["userId"]
        })
        .then(res => {
          if (res.code == 200) {
            if (res.body) {
              this.leftScore = res.body.leftScore;
              this.createCode(this.leftScore);
            } else {
              this.createCode(0);
            }
          } else {
            this.createCode(0);
          }
        });
    },

    createCode(leftScore) {
      let userInfo = this.$A.GS("userInfo");
      let params = {
        userId: userInfo.userId, //用户id
        userName: userInfo.userName, //用户名
        userNumber: userInfo.userNumber, //工号
        type: userInfo.type, //组织类型
        userType: userInfo.userType, //用户类型 1内部/2外部
        projecName:
          userInfo.type == 3
            ? this.$A.GS("orgInfo")["orgName"]
            : userInfo.orgName,
        //项目
        projecId:
          userInfo.type == 3 ? this.$A.GS("orgInfo")["orgId"] : userInfo.orgId,
        unitName: userInfo.type == 3 ? userInfo.orgName : null, //单位
        unitId: userInfo.type == 3 ? userInfo.orgId : null,
        leftScore: leftScore
      };
      this.content = JSON.stringify(params);
    }
  },
  destroyed() {}
};
</script>

<style lang="scss">
#IntegrationCode {
  .p-content {
    display: flex;
    //justify-content: center;
    align-items: center;
    background: #fff;
    dl {
      margin-top: 1.29rem;
      text-align: center;
      .des {
        font-size: 0.17rem;
        font-weight: 680;
        color: rgba(61, 61, 61, 1);
        margin-bottom: 0.08rem;
      }
      .fen {
        font-size: 0.13rem;
        font-weight: 400;
        color: rgba(140, 140, 140, 1);
      }
    }
  }
}
</style>
